<template>
  <div class="">
    <PageTitle title="卡券添加"></PageTitle>
    <div class="form-wrapper">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="所属活动" prop="activity_id">
          <el-select v-model="ruleForm.activity_id" placeholder="请选择所属活动">
            <template v-for="(item, index) in meal">
              <el-option :label="item.activity_name" :value="item.id" :key="index"></el-option>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="数量" prop="num">
          <el-input v-model="ruleForm.num" placeholder="请输入商品描述"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="$router.go(-1)">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  import _ from 'lodash'
  import PageTitle from '@/components/pageTitle/PageTitle.vue'
  export default {
    name: 'CouponDetail',
    components: {
      PageTitle,
    },
    activated() {
      this.$goTop()
      this._getMealList()
    },
    deactivated() {
      this.$destroy()
    },
    data() {
      return {
        meal: [],
        ruleForm: {
          activity_id: '',
          num: ''
        },
        rules: {
          activity_id: [
            { required: true, message: '请输入活动名称', trigger: 'blur' }
          ],
          num: [
            { required: true, message: '请输入数量', trigger: 'blur' },
            {
              validator(rule, value, callback) {
                const boolean = new RegExp(/^[0-9][0-9]*$/).test(value)
                if (!boolean) {
                  return callback(new Error('请输入正整数'))
                }
                return callback()
              },
              trigger: 'blur'
            }
          ],
        }
      }
    },
    methods: {
      // 重置表单
      resetForm(formName) {
        this.$refs[formName].resetFields()
      },
      async _getMealList() {
        let { data: { data, code } } = await this.$axios({
          url: this.$api + '/v1/activity/list',
          method: 'GET',
          params: {
            size: 1000,
            current: 1
          }
        })
        if(code == 0) {
          this.meal = data.items
        }
      },
      // 提交表单
      submitForm(formName) {
        this.$refs[formName].validate(valid => {
          if (!valid) {
            return false
          } else {
            this._submit()
          }
        })
      },
      _submit: _.throttle(async function() {
        let { data: { code, data } } = await this.$axios({
          url: this.$api + '/v1/code/ment',
          method: 'POST',
          data:{
            activity_id: this.ruleForm.activity_id,
            num: this.ruleForm.num
          }
        })
        if(code == 0) {
          this.$message.success(data)
          setTimeout(() => {
            this.$router.go(-1)
          }, 1500)
        }
      }, 2000, {trailing: false})
    }
  }
</script>

<style lang='scss'>
  .form-wrapper {
    width:50%;
  }
</style>
